function vd(obj) {
    var out = "";
    if(obj && typeof(obj) == "object"){
        for (var i in obj) {
            out += i + ": " + obj[i] + "\n";
        }
    } else {
        out = obj;
    }
    alert(out);
}

//------------------------------
var isDebug = true;

var widthHandly = null;
var countDoors = null;
var isMinusOverlapFor4Doors = false;
var widthAperture = null;
var widthTopPlank = null;
var widthBottomPlank = null;
var height = null;
var widthHProfile = null;
var widthDoor = null;
var scale = null;

var wc = null;
var controlPanel = null;
var workLayout = null;

var whiteAnimation = null;

function SetWidthHandly(widthHandlyT){
    var widthHandlyT = parseFloat(widthHandlyT);
    if (widthHandlyT < 0.005){
        if (isDebug) console.debug("Width of handly < 0.005 !");
        return null;
    }
    widthHandly = widthHandlyT;
    if (isDebug) console.debug("Set width of handly: " + widthHandly + " !");
}

function SetCountDoors(countDoorsT, isMinusOverlapFor4DoorsT){
    var countDoorsT = parseInt(countDoorsT);
    if (countDoorsT < 1){
        if (isDebug) console.debug("Count doors < 1 !");
        return null;
    }
    countDoors = countDoorsT;
    if (isDebug) console.debug("Set count doors: " + countDoors + " !");
    if (countDoors == 4 && isMinusOverlapFor4DoorsT == true){
        isMinusOverlapFor4Doors = true;
        if (isDebug) console.debug("Set -1 overlap !");
    }
}

function SetWidthAperture(widthApertureT){
    if (widthHandly ==  null){
        if (isDebug) console.debug("Not specified width handly !");
        return null;
    }
    if (countDoors ==  null){
        if (isDebug) console.debug("Not specified count doors !");
        return null;
    }
    var widthApertureT = parseFloat(widthApertureT);
    var minWidthApertureT = widthHandly * (countDoors + (isMinusOverlapFor4Doors ? 1 : 0));
    if (widthApertureT < minWidthApertureT){
        if (isDebug) console.debug("Given width is less than a minimum (" + minWidth + ") !");
        return null;
    }
    widthAperture = widthApertureT;
    if (isDebug) console.debug("Set width aperture: " + widthAperture + " !");
}

function SetHorizontalParams(widthHandlyT, countDoorsT, isMinusOverlapFor4DoorsT, widthApertureT){
    SetWidthHandly(widthHandlyT);
    SetCountDoors(countDoorsT, isMinusOverlapFor4DoorsT);
    SetWidthAperture(widthApertureT);
}

function SetWidthTopPlank(widthTopPlankT){
    var widthTopPlankT = parseFloat(widthTopPlankT);
    if (widthTopPlankT < 0.005){
        if (isDebug) console.debug("Width of top plank < 0.005 !");
        return null;
    };
    widthTopPlank = widthTopPlankT;
    if (isDebug) console.debug("Set width top plank: " + widthTopPlank + " !");
}

function SetWidthBottomPlank(widthBottomPlankT){
    var widthBottomPlankT = parseFloat(widthBottomPlankT);
    if (widthBottomPlankT < 0.005){
        if (isDebug) console.debug("Width of bottom plank < 0.005 !");
        return null;
    };
    widthBottomPlank = widthBottomPlankT;
    if (isDebug) console.debug("Set width bottom plank: " + widthBottomPlank + " !");
}

function SetHeightAperture(heightApertureT){
    if (widthTopPlank ==  null){
        if (isDebug) console.debug("Not specified width top plank !");
        return null;
    }
    if (widthBottomPlank ==  null){
        if (isDebug) console.debug("Not specified width bottom plank !");
        return null;
    }
    var heightApertureT = parseFloat(heightApertureT);
    var minHeightApertureT = widthTopPlank + widthBottomPlank;
    if (heightApertureT < minHeightApertureT){
        if (isDebug) console.debug("Given height is less than a minimum (" + minHeightApertureT + ") !");
        return null;
    }
    heightAperture = heightApertureT;
    if (isDebug) console.debug("Set height: " + this.heightAperture + " !");
}

function SetVerticalParams(widthTopPlankT, widthBottomPlankT, heightApertureT){
    SetWidthTopPlank(widthTopPlankT);
    SetWidthBottomPlank(widthBottomPlankT);
    SetHeightAperture(heightApertureT);
}

function SetWidthHProfile(widthHProfileT){
    var widthHProfileT = parseFloat(widthHProfileT);
    if (widthHProfileT < 0.005){
        if (isDebug) console.debug("Width of H-profile < 0.005 !");
        return null;
    };
    widthHProfile = widthHProfileT;
    if (isDebug) console.debug("Set width of H-profile: " + widthHProfile + " !");
}

function CalcWidthDoor(){
    var widthDoorT = widthAperture + widthHandly * (countDoors - (isMinusOverlapFor4Doors ? 2 : 1))
    widthDoorT = widthDoorT / countDoors;
    widthDoor = widthDoorT;
    if (isDebug) console.debug("Calc width door: " + widthDoor + " !");
}

function InitAperture(widthHandlyT, countDoorsT, isMinusOverlapFor4DoorsT,
widthApertureT, widthTopPlankT, widthBottomPlankT, heightApertureT, widthHProfileT){
    if (isDebug) console.debug("Start init aperture !");
    SetHorizontalParams(widthHandlyT, countDoorsT, isMinusOverlapFor4DoorsT, widthApertureT);
    SetVerticalParams(widthTopPlankT, widthBottomPlankT, heightApertureT);
    SetWidthHProfile(widthHProfileT);
    CalcWidthDoor();
    if (isDebug) console.debug("Finish init aperture !");
}


function CalcScale(widthApertureT, heightApertureT,
widthModelApertureT, heightModelApertureT){
	var coefWidthT = widthModelApertureT / widthApertureT;
	var coefHeightT = heightModelApertureT / heightApertureT;
	return (coefWidthT <= coefHeightT ? coefWidthT : coefHeightT);
}

function DialogHandlys(){
    if (isDebug) console.debug("Start Dialog Handly !");
    
    $.getJSON('materials/showhandlyspecs/format/json', function(data) {
        clearInterval(whiteAnimation);
        whiteAnimation = null;
        $(wc).empty();
        var handlySpecs = data['hanlySpecs'];
        if (handlySpecs.length > 0){
            for (var key in data['hanlySpecs']){
                
            }
        } else {
            
        }
        
        /*
        var blockDialog = document.createElement('div');
        blockDialog.setAttribute('class', 'dialog');
        
        var path = data.path_handly_specs;
        var c2 = '';
        $.each(data.handly_types, function(key, val) {
            c2 += '<li id="select_item-' + key + '">';           
                c2 += "<img src='" + path + val.file_name +"'>";
                c2 += "<div class='name_item'>" + val.name + "</div>";
            c2 += '</li>';
        });
        var c = "";
        c += "<div class='dialog_header'>Ручки</div>";
        
        if (c2 != ""){
            c += "<ul class='select_list' id='dialog_select_handly_type'>";
                c = c.concat(c2);
            c += "</ul>";
        } else {
            c += "<div class='dialog_data'>";
                c += "<div class='dialog_error'>Ручок нема !</div>";
            c += "</div>";
        }
            
    	$(blockDialog).html(c);
        $('#dialog_select_handly_type').selectable({
            selected: function(event, ui) {
                var number_type_handly = $(ui.selected).attr('id');
                number_type_handly = number_type_handly.split('-')[1];
                if (typeof number_type_handly !== 'undefined' &&
                    data.handly_types[number_type_handly] != null)
                {
                    real['doors']['handly_spec'] = data.handly_types[number_type_handly];
                    $(form_create_wall_hole).empty();
                    dialog_texture(form_create_wall_hole, real, model);
                }
            }
        });
        this.wc.append(blockDialog);
        */
    }).error(function(e) { DialogHandlys(); vd(e); });
    
    var animI = 1;
    if (whiteAnimation == null){
        wc.empty();
        var progressBlock = document.createElement('div');
        wc.append(progressBlock);
        progressBlock.setAttribute('class', 'progress_block');
        $(progressBlock).html('Loading ');
        whiteAnimation = setInterval(function(){
            animI++;
            if(animI > 4) {
                $(progressBlock).html('Loading ');
                animI = 1;
            } else {
                $(progressBlock).html($(progressBlock).html() + '.');
            }
        }, 800);
    }
}

function StartDialogs(){
    if (isDebug) console.debug("Start Dialogs !");
    DialogHandlys();
}

function InitWrapperCalculator(id, widthWrapperT){
    var wcT = $(id);
    if (wcT.length > 0){
        if (isDebug) console.debug("Wrapper calculator is find !");
        wc = wcT;
        
        StartDialogs();
        
        /*var controlPanelT = $(document.createElement('div'));
        this.controlPanel = controlPanelT;
        wcT.append(controlPanel);
        controlPanelT.width(widthWrapperT);
        controlPanelT.height(40);
        
        var workLayoutT = $(document.createElement('div'));
        this.workLayout = workLayoutT;
        wcT.append(workLayoutT);
        workLayoutT.width(widthWrapperT);
        workLayoutT.height(widthWrapperT/2);
        */
    } else {
        if (isDebug) console.debug("Wrapper calculator not find !");
    }
}

function StartCalculator(){
    if (isDebug) console.debug("\n ***");
    InitWrapperCalculator('#calculator', 975);
    //InitAperture(0.08, 4, true, 3, 0.1, 0.08, 1.8, 0.02);
}

(function($){
  jQuery.fn.ElasticPaginator = function(options){
    options = $.extend({
      currentPage: 1
    }, options);
    var make = function(){
        load(1);
        $(this).scroll(function(){
            load(1);
        });
        $(this).mousedown(function(){
            load(1);
        });
    };
    
    var load = function(offsetCurrentPage){
        $.getJSON('materials/showhandlyspecs/format/json', function(data) {
            $(this).html('sfdgf');
        })
    }
    
    return this.each(make); 
  };
})(jQuery);

(function($){
  jQuery.fn.FilterList = function(options){
    options = $.extend({
    }, options);
    var make = function(){
        $(this).keyup(function(e){
            var filterStr = $(this).val();
            if (filterStr != ""){
                if (options.loadAjaxThread != null){
                    options.loadAjaxThread.abort();
                }
                options.reload = $.getJSON(options.url, {filter: filterStr}, function(data) {
                    options.designFuncion(data);
                    options.loadAjaxThread = null;
                });
            }
        });
    };
    return this.each(make); 
  };
})(jQuery);

(function($){
  jQuery.fn.Calculator = function(options){
    options = $.extend({
    }, options);
    var make = function(){
        
    };
    return this.each(make); 
  };
})(jQuery);

$(function(){
    $('#calculator').Calculator();
    /*$('#test').FilterList({
        url: 'materials/showhandlyspecs/format/json',
        designFuncion: function(data){
            vd(data.hanlySpecs)
        }
    });*/
});